package com.example.fu3.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.fu3.model.User;
import com.example.fu3.service.impl.UserServiceImpl;
import com.example.fu3.utils.AjaxResult;
import com.example.fu3.utils.JWTUtil;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @ApiOperation("用户登录接口")
//    @ApiImplicitParams({
//    @ApiImplicitParam(name = "name", required = true, dataType = "String",value = "用户名"),
//    @ApiImplicitParam(name = "password", required = true, dataType = "String",value = "密码")
//    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功", response = AjaxResult.class)
    })
    @PostMapping("/login")
    public AjaxResult login(@Validated @RequestBody User u){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq(u.getName() != null,"user_name",u.getName());
        wrapper.eq(u.getPassword() != null,"password",u.getPassword());
        User findu = userService.getOne(wrapper);
        if (findu != null){
            System.out.println(findu.toString());
            String token = JWTUtil.createToken(findu);
            return AjaxResult.success(token);
        }else {
            return AjaxResult.error("用户不存在");
        }

    }

    @GetMapping("/test")
    public AjaxResult test(@RequestParam String s){
        return AjaxResult.success(s);
    }

    @GetMapping("/test1")
    @CrossOrigin
    public AjaxResult test1(){
        return AjaxResult.success("nsklfjdksfkdsfh");
    }

}
